// pages/addinfo/addinfo.js
const request=require('../../util/request.js')
const instruct = require('../../util/tspl')
import bleDisNetwork from '../../util/bleDisNetwork'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    list:[],
    __returnParams:false,
    selectedItems: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      __returnParams:false,

    })
    this.data.p_no = options.p_no
    this.data.page = 1
    this.getList()

  },
  
  getList(search="",date="") {
    var that = this;
    request.requestGet('order/get_department', '', "加载中",function (res) {
        if (res.status === 200) {
          
          that.setData({
            list: res.data,
          })
        }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    let name = `formData.${valueName}`;
    this.setData({
      [valueName]:event.detail
    });
  },
  onTabChange(event){
    if(event.detail.index === 1){
      this.data.datastatus = 1
    }else if(event.detail.index === 2){
      this.data.datastatus = 0
    }else if(event.detail.index === 0){
      this.data.datastatus = 2
    }
    this.data.page = 1
    this.getList()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.data.page = 1
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },

  onSearch(e){
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.getList(e.detail)
  },
  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },
  comment(event){
    let tmp = event.currentTarget.dataset.index
    let pic_no = ""
    for(let i = 0;i<this.data.projectbody.length;i++){
      if(this.data.projectbody[i].id === tmp){
        pic_no = this.data.projectbody[i].pic_no
      }
    }
    wx.navigateTo({
      url: `/pages/pic_no_list_process/pic_no_list_process?p_no=${tmp}&pic_no=${pic_no}`,
    })
  },

  setrem(e){
    wx.navigateTo({
      url: `/pages/pic_no_rember/pic_no_rember?FInterID=${e.currentTarget.dataset.index}&fbillno=${e.currentTarget.dataset.fbillno}`,
    })
  },

  onItemClick: function(e) {
    const clickedItem = e.currentTarget.dataset.item;
    const clickedIndex = e.currentTarget.dataset.index;
    const list = this.data.list;
    if (list[clickedIndex].selected) {
      this.setData({
        [`list[${clickedIndex}].selected`]: false
      });
      this.setData({
        selectedItems: []
      });
    } else {
      const updatedList = list.map(item => ({
        ...item,
        selected: false
      }));
      updatedList[clickedIndex].selected = true;
      this.setData({
        list: updatedList,
        selectedItems: [clickedItem]
      });
    }
  },

  submit(event){
    var that = this
    console.log(that.data.selectedItems,"that.data.selectedItems")
    wx.navigateBack({
      delta: 1,
      success: function() {
        // 确保页面完全激活后再处理数据
        setTimeout(() => {
          const currentPages = getCurrentPages();
          const currentPage = currentPages[currentPages.length - 1];
          currentPage.onSubmitSuccess(that.data.selectedItems);
        }, 200);
      }
    });
  },

  select(e){
    const pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    if (pages.length >= 2) {
      let tmplist = [e.currentTarget.dataset.index,e._relatedInfo.anchorRelatedText,this.data.selindex]
      pages[pages.length - 2].setData({
        __returnParams: tmplist
      }, () => {
        wx.navigateBack() // 确保setData回调完成后再返回
        prevPage.setManager()
      })
    }
  }

  

})